//
//  ScenicSearchDetailViewController.m
//  TravelToHelp
//
//  Created by lanou3g on 15/11/1.
//  Copyright © 2015年 冯贵宾. All rights reserved.
//

#import "ScenicSearchDetailViewController.h"
#import "FLSAttractionsQuery.h"

@interface ScenicSearchDetailViewController ()<MONActivityIndicatorViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) FLSAttractionsQuery *attractionQuery;
@property (nonatomic, strong) MONActivityIndicatorView *indicatorView;
@property (nonatomic, strong) AppDelegate *app;

@end

@implementation ScenicSearchDetailViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"reloadData" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _app = [[AppDelegate alloc] init];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self addIndicatorView];
    [self setUpData];
    [self setNaVigationBar];
    [self addInternetObserver];
}


// 通知网络状态
- (void)addInternetObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDataWithInternet:) name:@"reloadData" object:nil];
}

- (void)reloadDataWithInternet:(NSNotification *)notification
{
    [self setUpData];
}


- (void)setNaVigationBar
{
   self.navigationItem.title = @"景点详情";
  UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backNew.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector(actionLeftButton:)];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}
    
- (void)actionLeftButton:(UIBarButtonItem *)leftButton
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setUpData
{
    if ([_app internetReachableOrNot]) {
        
    if (self.model == nil) {
        [self.attractionQuery AttractionsQueryWithPlace:self.placeText Block:^(ScenicSearchModel *model) {
            if (model.state == YES) {
                self.model = model;
                [self.indicatorView stopAnimating];
                [self addScrollView];
            } else {
                [self.indicatorView stopAnimating];
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Sorry" message:@"抱歉了亲,你搜索的景点暂未收录" preferredStyle:(UIAlertControllerStyleAlert)];
                
                // 创建按钮
                UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                    [alert dismissViewControllerAnimated:YES completion:nil];
                }];
                [alert addAction:okAction];
                [self presentViewController:alert animated:YES completion:nil];
            }
        }];
    } else {
        [self.indicatorView stopAnimating];
        [self addScrollView];
    }
    } else {
        [self.indicatorView stopAnimating];
        [self alertController];
    }
}

- (void)alertController
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的当前网络中断或网络信号差, 请检查网络后重新进入!!" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
}


#pragma mark - 添加加载等待动画
- (void)addIndicatorView
{
    _indicatorView = [[MONActivityIndicatorView alloc] init];
    _indicatorView.center = self.view.center;
    _indicatorView.delegate = self;
    
    _indicatorView.numberOfCircles = 5; // 圆点个数
    _indicatorView.internalSpacing = 5; // 圆点之间的距离
    _indicatorView.radius = 8; // 圆点半径
    //    _indicatorView.delay = 0;
    
    [self.view addSubview:_indicatorView];
    [_indicatorView startAnimating];
}

#pragma mark - 加载等待动画的代理方法
- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView circleBackgroundColorAtIndex:(NSUInteger)index
{
    switch (index) {
        case 0:
            return [UIColor redColor];
            break;
        case 1:
            return [UIColor magentaColor];
            break;
        case 2:
            return [UIColor orangeColor];
            break;
        case 3:
            return [UIColor purpleColor];
            break;
        default:
            return [UIColor yellowColor];
            break;;
    }
}



- (void)addScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
      
    // 标题的lable
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, kScreenWidth, 30)];
    titleLable.textAlignment = NSTextAlignmentCenter;
    titleLable.font = [UIFont systemFontOfSize:23];
    titleLable.numberOfLines = 0;
    CGRect newFrame0 = titleLable.frame;
    newFrame0.size.height = [ScenicSearchDetailViewController cellHeightWithModel:self.model.name font:23];
    titleLable.frame = newFrame0;

    titleLable.text = self.model.name;
    [self.scrollView addSubview:titleLable];
    
    //介绍的lable
    UILabel *recommendLable = [[UILabel alloc] initWithFrame:CGRectMake(0, titleLable.bottom + 15, kScreenWidth, 25)];
    recommendLable.text = self.model.abstract;
    recommendLable.numberOfLines = 0;
    recommendLable.font = [UIFont systemFontOfSize:14];
    recommendLable.textAlignment = NSTextAlignmentCenter;
    CGRect newFrame = recommendLable.frame;
    newFrame.size.height = [ScenicSearchDetailViewController cellHeightWithModel:self.model.abstract font:14];
    recommendLable.frame = newFrame;
    [self.scrollView addSubview:recommendLable];
    
    // 图片
    UIImageView *detailImageView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - 255) / 2, recommendLable.bottom + 20, 255, 50)];
    detailImageView.image = [UIImage imageNamed:@"DetailTitle.png"];
    
    // 图片上的label
    UILabel *imageLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 17, 255, 16)];
    imageLable.text = @"        具       体       详       情";
    imageLable.backgroundColor = [UIColor whiteColor];
    [detailImageView addSubview:imageLable];
    [self.scrollView addSubview:detailImageView];
    
    // 概括的lable
    UILabel *descripLable = [[UILabel alloc] initWithFrame:CGRectMake(10, detailImageView.bottom + 20, kScreenWidth - 10, 30)];
    descripLable.font = [UIFont systemFontOfSize:22];
    descripLable.text = @"概括:";
    
    [self.scrollView addSubview:descripLable];
    // 概括详细
    UILabel *descripeDetailLable = [[UILabel alloc] initWithFrame:CGRectMake(10, descripLable.bottom, kScreenWidth - 10, 50)];
    descripeDetailLable.font = [UIFont systemFontOfSize:14];
    descripeDetailLable.text = self.model.Description;
    descripeDetailLable.numberOfLines = 0;
    CGRect newFrame1 = descripeDetailLable.frame;
    newFrame1.size.height = [ScenicSearchDetailViewController cellHeightWithModel:self.model.Description font:14];
    descripeDetailLable.frame = newFrame1;
    [self.scrollView addSubview:descripeDetailLable];
    
    // 开放时间
    UILabel *openLable = [[UILabel alloc] initWithFrame:CGRectMake(10, descripeDetailLable.bottom + 20, kScreenWidth - 10, 30)];
    openLable.font = [UIFont systemFontOfSize:22];
    openLable.text = @"开放时间:";
    [self.scrollView addSubview:openLable];
    // 开放时间详细
    UILabel *openDeatilLable = [[UILabel alloc] initWithFrame:CGRectMake(10, openLable.bottom, kScreenWidth - 10, 50)];
    openDeatilLable.font = [UIFont systemFontOfSize:14];
    openDeatilLable.numberOfLines = 0;
    openDeatilLable.text = self.model.open_time;
    CGRect newFrame4 = openDeatilLable.frame;
    newFrame4.size.height = [ScenicSearchDetailViewController cellHeightWithModel:self.model.open_time font:14];
    openDeatilLable.frame = newFrame4;
    [self.scrollView addSubview:openDeatilLable];
    
    // 门票价格
    UILabel *feeLable = [[UILabel alloc] initWithFrame:CGRectMake(10, openDeatilLable.bottom + 20, kScreenWidth - 10, 30)];
    feeLable.font = [UIFont systemFontOfSize:22];
    feeLable.text = @"门票价格:";
    [self.scrollView addSubview:feeLable];
    // 门票价格详细
    UILabel *feeDeatilLable = [[UILabel alloc] initWithFrame:CGRectMake(10, feeLable.bottom, kScreenWidth - 10, 50)];
    feeDeatilLable.font = [UIFont systemFontOfSize:14];
    feeDeatilLable.numberOfLines = 0;
    feeDeatilLable.text = self.model.price;
    CGRect newFrame5 = feeDeatilLable.frame;
    newFrame5.size.height = [ScenicSearchDetailViewController cellHeightWithModel:self.model.price font:14];;
    feeDeatilLable.frame = newFrame5;
    
    [self.scrollView addSubview:feeDeatilLable];
    
    self.scrollView.contentSize = CGSizeMake(kScreenWidth, feeDeatilLable.bottom + 80);
    
    [self.view addSubview:self.scrollView];
}

+ (CGFloat)cellHeightWithModel:(NSString *)str font:(NSInteger)font
{
    // 计算字符串高度
    // 字号一定与lable相等
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName];
    CGRect lableFrame = [str boundingRectWithSize:CGSizeMake(kScreenWidth , 100000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return lableFrame.size.height;
}

- (FLSAttractionsQuery *)attractionQuery
{
    if (!_attractionQuery) {
        _attractionQuery = [[FLSAttractionsQuery alloc] init];
    }
    return _attractionQuery;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
